﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;
using System.Xml;
using System.Linq;
using Project.Model;
using System.Collections.Generic;
using Project.Model.Rights;

public partial class Master_Left : AdminPage
{
    //Class_User obj = new Class_User();
    public string menuString = string.Empty;
    public string url = string.Empty;
    //public string strList = "";
    //string sysKey = String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["SystemModuleKey"]) ? "" : System.Configuration.ConfigurationManager.AppSettings["SystemModuleKey"];

    protected void Page_Load(object sender, EventArgs e)
    {
        //url = Request.ApplicationPath;
        if (!IsPostBack)
        {
            menuString = GetMenuStr(); 
        }

    }



    private string GetMenuStr()
    {
        DataTable dt = new DataTable();
        //if (UserList != null)
        //{
        //    string moduleid = obj.GetRootModuleID(sysKey);
        //    if (moduleid == string.Empty)
        //    {

        //    }
        //    else
        //    {

        //        dt = obj.GetUserAllModule(UserList.UserID);
        //        DataView dv = dt.DefaultView;
        //        dv.RowFilter = "deleteid like '%" + moduleid + "%'";
        //        dt = dv.ToTable();
        //    }
        //}

        //IQueryable<MODULE> list = ManagerFactory.MODULEManager.GetAll();
        //if (list != null && list.ToList<MODULE>().Count > 0)
        //{
        //    dt = ConvertToDataTable<MODULE>(list.ToList<MODULE>());

        //    DataView dv = dt.DefaultView;
        //    dv.RowFilter = "moduletype='2'";
        //    dv.Sort = " modulesort asc";
        //    DataTable newDt = dv.ToTable();

        //    for (int i = 0; i < newDt.Rows.Count; i++)
        //    {
        //        menuString += "<li id=\"menu" + i + "\" class=\"m_mod\">\n";
        //        menuString += "<a href=\"javascript:;\" class=\"m_mod_hd\" onclick=\"doMenu('subMenu" + i + "')\">" + newDt.Rows[i]["modulefriendlyname"].ToString() + "</a>\n";
        //        if (i == 0)
        //            menuString += "<ul class=\"m_mod_bd expanded\" id=\"subMenu" + i + "\">\n";
        //        else
        //            menuString += "<ul class=\"m_mod_bd collapsed\" id=\"subMenu" + i + "\">\n";

        //        //if (newDt.Rows[i]["isvml"].ToString() == "1")
        //        //{
        //        //    menuString += BindFlowVML("ProjectManage.vdt", dt, newDt.Rows[i]["moduleid"].ToString());
        //        //}
        //        //else
        //        //{
        //        GetChildMenuStr(dt, newDt.Rows[i]["moduleid"].ToString());
        //        //}
        //        menuString += "</ul>\n";
        //        menuString += "</li>\n";
        //    }
        //}
        return menuString;
    }

    private void GetChildMenuStr(DataTable dt, string moduleid)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = "parentmoduleid='" + moduleid + "'";
            dv.Sort = " modulesort asc";
            for (int i = 0; i < dv.ToTable().Rows.Count; i++)
            {
                menuString += ("<li><a href=\"" + url + dv.ToTable().Rows[i]["moduleurl"].ToString() + "\" target=\"mainFrame\" >" + dv.ToTable().Rows[i]["modulefriendlyname"].ToString() + " </a></li>\n");

            }
        }

    }

    //private string BindFlowVML(string vmlFileName, DataTable dt, string moduleid)
    //{



    //    XmlDocument XmlDoc = new XmlDocument();
    //    XmlDoc.Load(Request.PhysicalApplicationPath + "\\WorkFlow\\Config\\" + vmlFileName);

    //    XmlNamespaceManager nsmanager = new XmlNamespaceManager(XmlDoc.NameTable);
    //    nsmanager.AddNamespace("v", "urn:schemas-microsoft-com:vml");
    //    XmlNodeList nodelist = XmlDoc.SelectNodes("v:group/v:roundrect[@id!='']", nsmanager);

    //    if (dt != null && dt.Rows.Count > 0)
    //    {
    //        DataView dv = dt.DefaultView;
    //        dv.RowFilter = "parentmoduleid='" + moduleid + "'";
    //        dv.Sort = " modulesort asc";
    //        for (int i = 0; i < nodelist.Count; i++)
    //        {
    //            XmlAttribute objAtt = XmlDoc.CreateAttribute("bg");
    //            //有权限则加入动作
    //            string Url = string.Empty;
    //            foreach (DataRow dr in dt.Rows)
    //            {
    //                string nodeCode = string.Empty;
    //                if (ConfigurationManager.AppSettings[nodelist.Item(i).Attributes["id"].Value] != null)
    //                {
    //                    nodeCode = ConfigurationManager.AppSettings[nodelist.Item(i).Attributes["id"].Value].ToString();
    //                }
    //                if (nodeCode == dr["moduleid"].ToString())
    //                {
    //                    Url = dr["moduleurl"].ToString();
    //                    break;
    //                }
    //            }
    //            if (Url != string.Empty)
    //            {
    //                XmlAttribute Attr = XmlDoc.CreateAttribute("onclick");
    //                Attr.Value = "OnShapeClick(this,'" + Url.Replace("&amp;", "&") + "');";
    //                nodelist.Item(i).Attributes.Append(Attr);
    //                XmlAttribute wAttr = XmlDoc.CreateAttribute("workflowid");
    //                //wAttr.Value = sWorkFlowID;
    //                nodelist.Item(i).Attributes.Append(wAttr);

    //                objAtt.Value = "#87cefa";
    //                nodelist.Item(i).Attributes.Append(objAtt);
    //                nodelist.Item(i).Attributes["fillcolor"].Value = "#87cefa";
    //                nodelist.Item(i).Attributes["strokecolor"].Value = "#87cefa";

    //                strList += nodelist.Item(i).Attributes["id"].Value + ",";
    //            }
    //            else
    //            {
    //                objAtt.Value = "#dcdcdc";
    //                nodelist.Item(i).Attributes.Append(objAtt);
    //                nodelist.Item(i).Attributes["fillcolor"].Value = "#dcdcdc";
    //                nodelist.Item(i).Attributes["strokecolor"].Value = "#dcdcdc";
    //            }
    //        }
    //    }
    //    return Server.HtmlDecode(XmlDoc.InnerXml);
    //    //.Replace("&amp;", "&");
    //}

    /// <summary>
    /// Ilist<T> 转换成 DataSet
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public DataSet ConvertToDataSet<T>(IList<T> i_objlist)
    {
        if (i_objlist == null || i_objlist.Count <= 0)
        {
            return null;
        }

        DataSet ds = new DataSet();
        DataTable dt = new DataTable(typeof(T).Name);
        DataColumn column;
        DataRow row;

        System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
        foreach (T t in i_objlist)
        {
            if (t == null)
            {
                continue;
            }

            row = dt.NewRow();

            for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
            {
                System.Reflection.PropertyInfo pi = myPropertyInfo[i];

                string name = pi.Name;

                Type colType = pi.PropertyType;

                if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {
                    colType = colType.GetGenericArguments()[0];
                }


                if (dt.Columns[name] == null)
                {
                    column = new DataColumn(name, colType);
                    dt.Columns.Add(column);
                }

                row[name] = pi.GetValue(t, null) == null ? DBNull.Value : pi.GetValue(t, null);
            }

            dt.Rows.Add(row);
        }

        ds.Tables.Add(dt);

        return ds;
    }
}
